package fr.udata.server.dao.impl;

import javax.jdo.PersistenceManager;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

import fr.udata.server.PMF;
import fr.udata.server.dao.UserDao;
import fr.udata.server.model.User;
import fr.udata.shared.model.UserB;

public class UserDaoImpl extends UserDao {

	@Override
	public UserB saveUser(UserB userB) {
		try {
			User user = new User(userB);
			PersistenceManager pm = PMF.get().getPersistenceManager();
			pm.makePersistent(user);
			userB = user.getBean();
			return userB;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public UserB getUser(String email) {
		try {
			PersistenceManager pm = PMF.get().getPersistenceManager();
			Key key = KeyFactory.createKey(User.class.getSimpleName(), email);
			User user = pm.getObjectById(User.class, key);
			return user.getBean();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

	@Override
	public UserB deleteUser(String email) {
		// TODO Auto-generated method stub
		return null;
	}

}
